<?php
import("data.db.file.DbFileInfo");

class DbDocInfo extends DbFileInfo {
	
	public $className = "data.logic.file.DocInfo";
	const CLASS_NAME = "data.logic.file.DocInfo";
	
	const TB_TABLENAME	= "pd_doc_info";
	const TB_I			= 'fdoc';
	
	const TB_TITLE		= 'fdoc_title';
	const TB_RL_NAME		= 'fdoc_real_name';
	const TB_RP_NAME		= 'fdoc_repr_name';
	const TB_SRV_NAME		= 'fdoc_serv_name';
	const TB_SIZE			= 'fdoc_size';
	const TB_VERSION		= 'fdoc_version';
	const TB_STATUS			= 'fdoc_status';
	const TB_DESCRIPTION	= 'fdoc_description';
	const TB_CATEGORY		= 'fdoc_catalogue';
	const TB_LOCALE			= 'fdoc_locale';
	
	public static function _get_TABLENAME() {return self::TB_TABLENAME;}
	public static function _get_ID() {return self::TB_I."_id";}
	public static function _get_SUFFIX() {return self::TB_I;}
	
	public $title;
	
	function forwardForeignCollection() {
		return parent::forwardForeignCollection();
	}
	function backwardForeignCollection() {
		return array("DocLink"=>"data.logic.file.DocLink");
	}
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function setTitle($value) {$this->title = $value;}
	function getTitle() {return $this->title;}
	
	function setItems(&$items) {
		$this->backwardForeigns["DocLink"]->setList($items);
	}
	function getItems() {
		return $this->backwardForeigns["DocLink"]->getList();
	}
	
	// restore
	function fillEntity($arrResult) {
		DbAbstractTable::fillEntity($arrResult);
		$this->setTitle(stripslashes($arrResult[self::TB_TITLE]));
		$this->setRealName(stripslashes($arrResult[self::TB_RL_NAME]));
		$this->setRepName(stripslashes($arrResult[self::TB_RP_NAME]));
		$this->setServName(stripslashes($arrResult[self::TB_SRV_NAME]));
		$this->setSize($arrResult[self::TB_SIZE]);
		$this->setVersion($arrResult[self::TB_VERSION]);
		$this->setStatus($arrResult[self::TB_STATUS]);
		$this->setDescription(stripslashes($arrResult[self::TB_DESCRIPTION]));
		$this->setCategoryId($arrResult[self::TB_CATEGORY]);
		$this->setLocale(DbFactory::getLocale($arrResult[self::TB_LOCALE]));
	}
	
	//store
	function fillFields() {
		DbAbstractTable::fillFields();
		$this->fildsForStore[self::TB_TITLE] = "'".addslashes($this->getTitle())."'";
		$this->fildsForStore[self::TB_RL_NAME] = "'".addslashes($this->getRealName())."'";
		$this->fildsForStore[self::TB_RP_NAME] = "'".addslashes($this->getRepName())."'";
		$this->fildsForStore[self::TB_SRV_NAME] = "'".addslashes($this->getServName())."'";
		$this->fildsForStore[self::TB_SIZE] = "'".$this->getSize()."'";
		$this->fildsForStore[self::TB_VERSION] = "'".$this->getVersion()."'";
		$this->fildsForStore[self::TB_STATUS] = "'".$this->getStatus()."'";
		$this->fildsForStore[self::TB_DESCRIPTION] = "'".addslashes($this->getDescription())."'";
		$this->fildsForStore[self::TB_CATEGORY] = "'".$this->getCategoryId()."'";
		if ($this->locale) $this->fildsForStore[self::TB_LOCALE] = "'".$this->locale->getId()."'";
	}
	
	function store() {
		$item_list = $this->getItems();
		DbAbstractTable::__executeQuery("delete from pd_doc_storage where dst_id_fdoc='".$this->getId()."'");
		foreach ($item_list as $item) {
			$item->setId("");
			$item->store();
		}
		DbAbstractTable::store();
	}
	
	function findFirstByStatus($status) {
		$arr = parent::__selectObjects(
			"select * from ".self::TB_TABLENAME." where fdoc_status='".$status."' order by fdoc_chdate desc limit 1",
			self::CLASS_NAME);
		if ($arr && count($arr)==1) return $arr[0];
		return null;
	}
	
	function findAllByStatus($status) {
		return parent::__selectObjects(
			"select * from ".self::TB_TABLENAME." where fdoc_status='".$status."'",
			self::CLASS_NAME);
	}
	
	function findAllByCategory($category, $local=null) {
		//TODO: category name must be replaced by category id
		return parent::__selectAllByCriteria(
			self::TB_CATEGORY."='".$category."'", self::CLASS_NAME, $local);
	}
	
	// same for all classes
	public static function findAll($local=null) {return DbAbstractTable::findAll(self::CLASS_NAME, $local);}
    public static function findById($id, $local=null) {return DbAbstractTable::findById($id, self::CLASS_NAME, $local);}
}
?>